package com.yanqu.road.server.cmd.activity.celebration;

import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.config.goods.OpenGoodsBagResult;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.log.LogCelebrationProcess;
import com.yanqu.road.pb.celebration.CelebrationProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.activity.celebration.CelebrationMgr;
import com.yanqu.road.server.manger.config.GoodsBagMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

/**
 * @Description 庆典使用道具
 * @Author cwq
 * @Data 2021/7/12 16:08
 */
@Cmd(code = GameProtocol.S_CELEBRATION_USE_PROP_CROSS_TO_GAME, desc = "庆典使用道具 跨服返回区服 协议")
public class CelebrationUsePropFromCrossCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        CelebrationProto.CelebrationUsePropCrossToGameMsg crossToGameMsg = CelebrationProto.CelebrationUsePropCrossToGameMsg.parseFrom(packet.getBytes());
        Property property = new Property(crossToGameMsg.getPropId(), crossToGameMsg.getNum());
        player.getModule(PreReduceModule.class).restorePreReduce(property);
        CelebrationProto.CelebrationUsePropRespMsg.Builder builder = CelebrationProto.CelebrationUsePropRespMsg.newBuilder();
        if(crossToGameMsg.getRet() == 0){
            // 扣道具
            player.getModule(CurrencyModule.class).removeCurrency(property, eLogMoneyType.CelebrationActivity, eLogMoneyType.CelebrationUsePropConsume);
            GoodsInfo goodsInfo = GoodsMgr.getGoodsById(crossToGameMsg.getPropId());
            long addSumScore = goodsInfo.getParamList().get(0).intValue() * crossToGameMsg.getNum();
            // 加排行
            player.notifyListener(eGamePlayerEventType.CelebrationActivityScoreRank.getValue(), addSumScore);
            player.notifyListener(eGamePlayerEventType.CelebrationActivityScoreCrossRank.getValue(), addSumScore);
            // 加奖励
            Property reward = new Property();
            for (int i = 0; i < crossToGameMsg.getNum(); i++) {
                OpenGoodsBagResult result = GoodsBagMgr.getRandomGoods(crossToGameMsg.getPropId());
                reward.addProperty(result.getReward());
            }
            player.getModule(CurrencyModule.class).addCurrency(reward, eLogMoneyType.CelebrationActivity, eLogMoneyType.CelebrationUsePropPropReward);

            builder.setReward(PropertyHelper.parsePropertyToString(reward));

            for (CelebrationProto.CelebrationProgress item :  crossToGameMsg.getAddProgressList()){
                // 加日志
                long curValue = item.getCurValue();
                LogMgr.addLogCelebrationProcess(new LogCelebrationProcess(CelebrationMgr.getActivityInfo().getActivityId(), player.getUserId(), item.getId(), item.getScore(), curValue, crossToGameMsg.getLogTime()));
            }
            builder.addAllAddProgress(crossToGameMsg.getAddProgressList());

            player.notifyListener(eGamePlayerEventType.CelebrationActivityFestiveCrossTotalValue.getValue(), crossToGameMsg.getCurLv());
        }
        builder.setRet(crossToGameMsg.getRet());
        player.sendPacket(ClientProtocol.U_CELEBRATION_USE_PROP, builder);
    }

    @Override
    public void sendData(GamePlayer player) {
    }
}
